//
//  SpePropertyView.m
//  JiaoX
//
//  Created by yinqy on 15/1/12.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "SpePropertyView.h"
#define LABEL_FONT_SIZE 13

@implementation SpePropertyView

-(instancetype)initWithArray:(NSArray *)array
{
    self = [super init];
    if (self) {
        UIView *splite = [[UIView alloc]init];
        [splite setFrame:CGRectMake(JXPublicInset, 0, JXScreenW-JXPublicInset, 0.5)];
        [splite setBackgroundColor:SEPARATORCOLOR];
        [self addSubview:splite];
        if (array!=nil && array.count!=0) {
            CGFloat currentLeft = JXPublicInset;
            CGFloat currentTop = 15;
            CGFloat screen2 = JXScreenW/2;
            CGFloat maxWidth = (JXScreenW-JXPublicInset*2)/2;
            
            CGFloat selfHeight = 0;
            
            for (int i=0; i<array.count;i++) {
                NSString *str = array[i];
                CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE] constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
                
                UILabel *label = [[UILabel alloc]init];
                [label setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE]];
                [label setTextColor:JXColor(102, 102, 102)];
                CGFloat labelX = 0;
                CGFloat labelY = 0;
                
                [label setText:str];
                
                //1.如果当前就在最左边开始
                if(currentLeft == JXPublicInset){
                    labelX = currentLeft;
                    labelY = currentTop;
                    
                    if (size.width+labelX>screen2) {
                        //
                        currentLeft = JXPublicInset;
                        currentTop = currentTop + JXPublicInset+size.height;
                    }else{
                        currentLeft = screen2;
                        //currentTop不改变
                    }
                }else{
                    //如果当前是在最中间开始
                    //1，先判断宽度是否大于maxWidth
                    if (size.width<maxWidth) {
                        labelX = currentLeft;
                        labelY = currentTop;
                        
                        currentLeft = JXPublicInset;
                        currentTop = currentTop +JXPublicInset +size.height;
                        
                    }else{
                        currentLeft = JXPublicInset;
                        currentTop = currentTop+JXPublicInset+size.height;
                        labelX = currentLeft;
                        labelY = currentTop;
                        currentTop = currentTop+JXPublicInset+size.height;
                    }
                }
                
                if(i==array.count-1){
                    selfHeight = labelY+LABEL_FONT_SIZE+15;
                }
                [label setFrame:CGRectMake(labelX, labelY, size.width, label.font.lineHeight)];
                
                [self addSubview:label];
            }
            self.bounds = CGRectMake(0, 0, JXScreenW, selfHeight);
        }else{
            [splite removeFromSuperview];
        }
    }
    return self;
}

@end
